package com.ms.oschina.viewholder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.Friend;
import com.ms.oschina.ui.activity.NewsDetailsActivity;
import com.ms.oschina.ui.activity.UserActivity;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.view.CircleImageView;
import com.squareup.picasso.Picasso;

public class FriendViewHolder extends BaseViewHolder<Friend> {
	private CircleImageView civFriendUserAvatar;
	private TextView tvFriendUserName;
	private ImageView ivMyGender;
	private TextView tvFriendLocation;
	private TextView tvFriendDescribe;
	private Friend data;

	/**
	 * Find the Views in the layout<br />
	 * <br />
	 * Auto-created on by Android Layout Finder
	 * (http://www.buzzingandroid.com/tools/android-layout-finder)
	 * 
	 * @param view
	 */
	private void findViews(View view) {
		civFriendUserAvatar = (CircleImageView) view
				.findViewById(R.id.civ_aboutme_userAvatar);
		tvFriendUserName = (TextView) view
				.findViewById(R.id.iv_event_title);
		ivMyGender = (ImageView) view.findViewById(R.id.iv_my_gender);
		tvFriendLocation = (TextView) view
				.findViewById(R.id.iv_event_time);
		tvFriendDescribe = (TextView) view
				.findViewById(R.id.tv_friend_describe);
	}

	@Override
	public View initView() {
		View view = UiUtils.inflate(R.layout.item_friend);
		findViews(view);
		return view;
	}

	@Override
	public void initEvent() {
		civFriendUserAvatar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Context context = UiUtils.getContext();
				Intent intent = new Intent(context, UserActivity.class);
				intent.putExtra(Constants.USER_ID, data.getId());
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});
	}

	@Override
	public void setData(int position, Friend data) {
		this.data = data;
		String portrait = data.getPortrait();
		if (!TextUtils.isEmpty(portrait)) {
			Picasso.with(UiUtils.getContext()).load(portrait)
					.placeholder(R.drawable.widget_dface)
					.into(civFriendUserAvatar);
		}

		tvFriendDescribe.setText(data.getExpertise());
		tvFriendLocation.setText(data.getFrom());
		tvFriendUserName.setText(data.getName());
	}

}
